﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    public class LinkCreateEdit : SkinnedWebControl
    {
        private TreeCallBackDropDownList ddlItem;
        private TextBox tbxTitle;
        private TextBox tbxUrl;
        private ImageUpload imgLogo;
        private TextBox tbxDescription;
        private Image imgLogoPreview;
        private CheckBox cbxState;

        private Button btnTool;
        private Button btnConfirm;

        protected override void InitializeSkin(Control skin)
        {
            ddlItem = (TreeCallBackDropDownList)skin.FindControl("ddlItem");
            tbxTitle = (TextBox)skin.FindControl("tbxTitle");
            tbxUrl = (TextBox)skin.FindControl("tbxUrl");
            imgLogo = (ImageUpload)skin.FindControl("imgLogo");
            tbxDescription = (TextBox)skin.FindControl("tbxDescription");
            cbxState = (CheckBox)skin.FindControl("cbxState");
            imgLogoPreview = (Image)skin.FindControl("imgLogoPreview");

            btnConfirm = (Button)skin.FindControl("btnConfirm");
            btnTool = (Button)skin.FindControl("btnTool");
            ddlItem.BaseNodeId = SiteContext.Current.BaseNodeId;

            imgLogoPreview.Visible = false;

            btnConfirm.Click += new EventHandler(btnConfirm_Click);

            if (Id > 0 && !Page.IsPostBack)
                Info = Links.GetLink(Id);
        }

        void btnConfirm_Click(object sender, EventArgs e)
        {
            LinkInfo info = Info;
            if (imgLogo.HasFile)
            {
                info.ImageUrl = imgLogo.SaveAs();
                if (string.IsNullOrEmpty(info.ImageUrl))
                    return;
            }

            if (Id <= 0)
                Links.Insert(info);
            else
                Links.Update(info);

        }

        private LinkInfo Info
        {
            get
            {
                LinkInfo info = new LinkInfo();
                info.Id = Id;
                info.ItemId = ddlItem.SelectValue;
                info.Url = tbxUrl.Text.Trim();
                info.Title = tbxTitle.Text.Trim();
                info.Description = tbxDescription.Text.Trim();
                info.Status = cbxState.Checked ? StatusType.Enabled : StatusType.Disabled;

                return info;
            }
            set
            {
                if (value == null)
                    return;

                EnsureChildControls();

                ddlItem.SelectValue = value.ItemId;
                tbxTitle.Text = value.Title;
                tbxUrl.Text = value.Url;
                tbxDescription.Text = value.Description;
                cbxState.Checked = value.Status == StatusType.Enabled;

                if (!string.IsNullOrEmpty(value.ImageUrl))
                {
                    imgLogoPreview.Visible = true;
                    imgLogoPreview.ImageUrl = value.ImageUrl;
                }
            }
        }

        private int Id
        {
            get { return SiteContext.Current.Id; }
        }

        protected override string DefaultSkinFilename
        {
            get { return "Link/LinkCreateEdit.ascx"; }
        }
    }
}
